home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / source / rclesrc.10 / ircle sources / IRCChannels.p < prev    next >
Encoding:
Text File  |  1992-09-06  |  7.7 KB  |  379 lines

  1. {    ircle - Internet Relay Chat client    }
  2. {    File: IRCChannels    }
  3. {    Copyright ⌐ 1992 Olaf Titz (s_titz@iravcl.ira.uka.de)    }
  4.  
  5. {    This program is free software; you can redistribute it and/or modify    }
  6. {    it under the terms of the GNU General Public License as published by    }
  7. {    the Free Software Foundation; either version 2 of the License, or    }
  8. {    (at your option) any later version.    }
  9.  
  10. {    This program is distributed in the hope that it will be useful,    }
  11. {    but WITHOUT ANY WARRANTY; without even the implied warranty of    }
  12. {    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    }
  13. {    GNU General Public License for more details.    }
  14.  
  15. {    You should have received a copy of the GNU General Public License    }
  16. {    along with this program; if not, write to the Free Software    }
  17. {    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.    }
  18.  
  19. unit IRCChannels;
  20. { Deals with the window-per-channel interface and all messages to the user. }
  21.  
  22. interface
  23.  
  24. uses
  25.     TCPTypes, TCPStuff, TCPConnections, ApplBase, MsgWindows, IRCGlobals, IRCaux;
  26.  
  27. procedure InitIRCChannels;
  28. { Startup }
  29.  
  30. procedure LineMsg (var s: string);
  31. { Display message in main window }
  32.  
  33. procedure StatusMsg (n: integer);
  34. { Display message from STR# 256 }
  35.  
  36. procedure SetMainTitle (var newtitle: string);
  37. { Call this after changing nick }
  38.  
  39. function DoJoin (var ch: string): MWHndl;
  40. { Open channel window for ch -- this may as well be a queried nick }
  41.  
  42. procedure DoPart (var ch: string);
  43. { Close channel window for ch }
  44.  
  45. procedure partWindow (p: WindowPtr);
  46. {user closing window}
  47.  
  48. procedure ChannelMsg (var ch, msg: string);
  49. { Display msg in the window for channel ch, }
  50. { or in the frontmost window if a ch window does not exist }
  51.  
  52. procedure Message (var msg: string);
  53. { Display msg in current window }
  54.  
  55. implementation
  56.  
  57. type
  58.     CPtr = ^Clist;
  59.     Clist = record
  60.             n: CPtr;
  61.             w: MWHndl;
  62.             mp: integer;
  63.         end;
  64.  
  65.  
  66. { Notification Manager glue - not needed on new compilers }
  67. type
  68.     NMRec = record
  69.             qLink: QElemPtr;   {next queue entry}
  70.             qType: INTEGER;    {queue type -- ORD(nmType) = 8}
  71.             nmFlags: INTEGER;    {reserved}
  72.             nmPrivate: LONGINT;    {reserved}
  73.             nmReserved: INTEGER;    {reserved}
  74.             nmMark: INTEGER;    {item to mark in Apple menu}
  75.             nmSIcon: Handle;     {handle to small icon}
  76.             nmSound: Handle;     {handle to sound record}
  77.             nmStr: StringPtr;  {string to appear in alert}
  78.             nmResp: ProcPtr;    {pointer to response routine}
  79.             nmRefCon: LONGINT;    {for application use}
  80.         end;
  81.     NMPtr = ^NMRec;
  82.  
  83. var
  84.     mwin: MWHndl;
  85.     CW: Cptr;
  86.     noti: NMRec;
  87.     SIcon, Sound: Handle;
  88.  
  89. function NMInstall (nmReqPtr: QElemPtr): OSErr;
  90. inline
  91.     $205F, $A05E, $3E80;
  92.  
  93. function NMRemove (nmReqPtr: QElemPtr): OSErr;
  94. inline
  95.     $205F, $A05F, $3E80;
  96.  
  97. function SetCurrentA5: longint;
  98. inline
  99.     $2E8D, $2A78, $0904;
  100.  
  101. function SetA5 (newA5: longint): longint;
  102. inline
  103.     $2F4D, $0004, $2A5F;
  104.  
  105. procedure remove (q: NMPtr);
  106.     var
  107.         ca5: longint;
  108.     begin
  109.         ca5 := setA5(q^.nmRefCon);
  110.         if notified then
  111.             notified := (NMRemove(@noti) <> 0);
  112.         ca5 := setA5(ca5);
  113.     end;
  114.  
  115. procedure NotifyUser (blink, beep, bkgnd: boolean);
  116.     begin
  117.         if blink or beep then begin
  118.             if blink then
  119.                 noti.nmSIcon := SIcon
  120.             else
  121.                 noti.nmSIcon := nil;
  122.             if beep then
  123.                 noti.nmSound := Sound
  124.             else
  125.                 noti.nmSound := nil;
  126.             if bkgnd then
  127.                 noti.nmResp := nil
  128.             else
  129.                 noti.nmResp := @remove;
  130.             if not notified then
  131.                 notified := (NMInstall(@noti) = 0);
  132.         end;
  133.     end;
  134.  
  135. procedure hideit (w: WindowPtr);
  136.     begin
  137.         HideWindow(w)
  138.     end;
  139.  
  140. function DoMSGWindow: boolean;
  141.     begin
  142.         if mwin = nil then begin
  143.             mwin := NewMWindow(CurrentNick, @hideit);
  144.             DoMsgWindow := false
  145.         end
  146.         else
  147.             DoMsgWindow := (mwin^^.w <> FrontWindow);
  148.         ShowWindow(mwin^^.w);
  149.         SelectWindow(mwin^^.w);
  150.     end;
  151.  
  152. procedure CommonMsg (var m: string; noti: boolean);
  153.     begin
  154.         if logging then
  155.             writeln(logfile, m);
  156.         if noti then
  157.             if inBackground then
  158.                 NotifyUser(default^^.notify[3], default^^.notify[4], true)
  159.             else
  160.                 NotifyUser(default^^.notify[1], default^^.notify[2], false);
  161.     end;
  162.  
  163. procedure LineMsg (var s: string);
  164.     var
  165.         b: boolean;
  166.     begin
  167.         b := DOMSGWindow;
  168.         MWMessage(mwin, s);
  169.         lastWindow := mwin;
  170.         CommonMsg(s, b);
  171.     end;
  172.  
  173. procedure StatusMsg (n: integer);
  174.     var
  175.         s: str255;
  176.         b: boolean;
  177.     begin
  178.         b := DOMsgWindow;
  179.         SysBeep(10);
  180.         GetIndString(s, 256, n);
  181.         MWMessage(mwin, s);
  182.     end;
  183.  
  184. procedure SetMainTitle (var newtitle: string);
  185.     begin
  186.         if mwin <> nil then
  187.             SetWTitle(mwin^^.w, newtitle);
  188.         SetItem(GetMHandle(M_WINDOWS), M_WI_MAIN, newtitle);
  189.         EnableItem(GetMHandle(M_WINDOWS), M_WI_MAIN);
  190.     end;
  191.  
  192.  
  193. function ChannelWindow (var ch: string): MWHndl;
  194.     var
  195.         s: Str255;
  196.         l: CPtr;
  197.     begin
  198.         l := CW;
  199.         while l <> nil do begin
  200.             GetWTitle(l^.w^^.w, s);
  201.             if EqualString(ch, s, false, true) then begin
  202.                 ChannelWindow := l^.w;
  203.                 exit(ChannelWindow)
  204.             end;
  205.             l := l^.n
  206.         end;
  207.         ChannelWindow := nil
  208.     end;
  209.  
  210.  
  211. function activate (var e: EventRecord): boolean;
  212.     var
  213.         p: CPtr;
  214.         s: string;
  215.     begin
  216.         activate := false;
  217.         if odd(e.modifiers) then begin
  218.             p := CW;
  219.             while p <> nil do begin
  220.                 if p^.w^^.w = WindowPtr(e.message) then begin
  221.                     GetWTitle(p^.w^^.w, CurrentTarget);
  222.                     UpdateStatusLine;
  223.                     exit(activate)
  224.                 end;
  225.                 p := p^.n
  226.             end;
  227.             currentTarget := '';
  228.             UpdateStatusLine;
  229.         end
  230.     end;
  231.  
  232. function Switcher (var e: EventRecord): boolean;
  233.     var
  234.         i: integer;
  235.     begin
  236.         inBackground := (bitand(e.message, 1) = 0);
  237.         if not InBackground then
  238.             InitCursor;
  239.         if notified then
  240.             i := NMRemove(@noti);
  241.         notified := false;
  242.         Switcher := false
  243.     end;
  244.  
  245. function wmenu (var e: EventRecord): boolean;
  246.     var
  247.         l: CPtr;
  248.     begin
  249.         case e.message of
  250.             M_WI_CYCLE: 
  251.                 SendBehind(FrontWindow, nil);
  252.             M_WI_MAIN: 
  253.                 if mwin <> nil then begin
  254.                     ShowWindow(mwin^^.w);
  255.                     SelectWindow(mwin^^.w);
  256.                 end;
  257.             otherwise
  258.                 begin
  259.                 l := CW;
  260.                 while l <> nil do begin
  261.                     if l^.mp = e.message then begin
  262.                         SelectWindow(l^.w^^.w);
  263.                         leave
  264.                     end;
  265.                     l := l^.n;
  266.                 end;
  267.                 wmenu := true
  268.             end
  269.         end
  270.     end;
  271.  
  272. procedure partWindow (p: WindowPtr); {user closing window}
  273.     var
  274.         p0, p1: CPtr;
  275.         s: str255;
  276.     begin
  277.         GetWTitle(p, s);
  278.         if IsChannel(s) then begin
  279.             s := concat('PART ', s);
  280.             PutLine(s);
  281.         end
  282.         else { query window }
  283.             DeleteMWindow(MWHndl(GetWRefCon(p)));
  284.     end;
  285.  
  286. function DoJoin (var ch: string): MWHndl; {callback from server}
  287.     var
  288.         w: MWHndl;
  289.         l: CPtr;
  290.     begin
  291.         w := ChannelWindow(ch);
  292.         if w = nil then begin
  293.             w := NewMWindow(ch, @partWindow);
  294.             InsMenuItem(GetMHandle(M_WINDOWS), ch, 255);
  295.             new(l);
  296.             l^.n := CW;
  297.             CW := l;
  298.             l^.w := w;
  299.             l^.mp := CountMItems(GetMHandle(M_WINDOWS));
  300.         end
  301.         else
  302.             SelectWindow(w^^.w);
  303.         DoJoin := w
  304.     end;
  305.  
  306. procedure DoPart (var ch: string); {callback from server}
  307.     var
  308.         l, l0: CPtr;
  309.         n: integer;
  310.         s: Str255;
  311.     begin
  312.         l := CW;
  313.         while l <> nil do begin
  314.             GetWTitle(l^.w^^.w, s);
  315.             if ch = s then begin
  316.                 n := l^.mp;
  317.                 DelMenuItem(GetMHandle(M_WINDOWS), n);
  318.                 DeleteMWindow(l^.w);
  319.                 if l = CW then
  320.                     CW := l^.n
  321.                 else
  322.                     l0^.n := l^.n;
  323.                 leave;
  324.             end;
  325.             l0 := l;
  326.             l := l0^.n
  327.         end;
  328.         l := CW;
  329.         while l <> nil do begin
  330.             if l^.mp > n then
  331.                 l^.mp := pred(l^.mp);
  332.             l := l^.n
  333.         end;
  334.     end;
  335.  
  336. procedure ChannelMsg (var ch, msg: string);
  337.     var
  338.         m: MWHndl;
  339.         b: boolean;
  340.     begin
  341.         m := ChannelWindow(ch);
  342.         if m = nil then begin
  343.             m := ChannelWindow(CurrentTarget);
  344.             if m = nil then begin
  345.                 b := DOMSGWindow;
  346.                 m := mwin
  347.             end;
  348.         end;
  349.         lastWindow := m;
  350.         MWMessage(m, msg);
  351.         CommonMsg(msg, m^^.w <> FrontWindow);
  352.     end;
  353.  
  354. procedure Message (var msg: string);
  355.     begin
  356.         ChannelMsg(CurrentTarget, msg)
  357.     end;
  358.  
  359.  
  360. procedure InitIRCChannels;
  361.     var
  362.         i: integer;
  363.     begin
  364.         mwin := nil;
  365.         CW := nil;
  366.         i := ApplTask(@activate, activateEvt);
  367.         i := ApplTask(@Switcher, app4Evt);
  368.         i := ApplTask(@wmenu, menuMsg + M_WINDOWS);
  369.         SIcon := GetResource('SICN', 128);
  370.         Sound := GetResource('snd ', 128);
  371.         with noti do begin
  372.             qType := 8;
  373.             nmMark := 1;
  374.             nmStr := nil;
  375.             nmRefCon := SetCurrentA5;
  376.         end;
  377.     end;
  378.  
  379. end.